﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using GeneralLibs;

namespace Lists
{
    public class HoSoKhamBenh : DoiTuong
    {
        #region Guid PhieuDangKiID property
        public Guid PhieuDangKiID
        {
            get
            {
                if (_DataRow["PhieuDangKiID"] is Guid) return (Guid)_DataRow["PhieuDangKiID"];
                return Guid.Empty;
            }
            set
            {
                _DataRow["PhieuDangKiID"] = value;
            }
        }
        #endregion

        #region string Ma property
        public string Ma
        {
            get
            {
                if (_DataRow["Ma"] is string) return (string)_DataRow["Ma"];
                return "";
            }
            set
            {
                _DataRow["Ma"] = value;
            }
        }
        #endregion

        #region DateTime Ngay property
        public DateTime Ngay
        {
            get
            {
                return (DateTime)DataRow["Ngay"];
            }
            set
            {
                _DataRow["Ngay"] = value;
            }
        }
        #endregion

        #region Guid BacSiKhamID property
        public Guid BacSiKhamID
        {
            get
            {
                if (_DataRow["BacSiKhamID"] is Guid) return (Guid)_DataRow["BacSiKhamID"];
                return Guid.Empty;
            }
            set
            {
                _DataRow["BacSiKhamID"] = value;
            }
        }
        #endregion

        #region Guid PhongBanID property
        public Guid PhongBanID
        {
            get
            {
                if (_DataRow["PhongBanID"] is Guid) return (Guid)_DataRow["PhongBanID"];
                return Guid.Empty;
            }
            set
            {
                _DataRow["PhongBanID"] = value;
            }
        }
        #endregion

        #region Guid NguoiLamPhieuID property
        public Guid NguoiLamPhieuID
        {
            get
            {
                if (_DataRow["NguoiLamPhieuID"] is Guid) return (Guid)_DataRow["NguoiLamPhieuID"];
                return Guid.Empty;
            }
            set
            {
                _DataRow["NguoiLamPhieuID"] = value;
            }
        }
        #endregion

        #region Guid BenhID property
        public Guid BenhID
        {
            get
            {
                if (_DataRow["BenhID"] is Guid) return (Guid)_DataRow["BenhID"];
                return Guid.Empty;
            }
            set
            {
                _DataRow["BenhID"] = value;
            }
        }
        #endregion

        #region string KetLuan property
        public string KetLuan
        {
            get
            {
                if (_DataRow["KetLuan"] is string) return (string)_DataRow["KetLuan"];
                return "";
            }
            set
            {
                _DataRow["KetLuan"] = value;
            }
        }
        #endregion

        #region string CanDan property
        public string CanDan
        {
            get
            {
                if (_DataRow["CanDan"] is string) return (string)_DataRow["CanDan"];
                return "";
            }
            set
            {
                _DataRow["CanDan"] = value;
            }
        }
        #endregion

        #region decimal NgayKhamLai property
        public decimal NgayKhamLai
        {
            get
            {
                if (_DataRow["NgayKhamLai"] is decimal) return (decimal)_DataRow["NgayKhamLai"];
                return 0;
            }
            set
            {
                _DataRow["NgayKhamLai"] = value;
            }
        }
        #endregion

        #region string TienSuBanThan property
        public string TienSuBanThan
        {
            get
            {
                if (_DataRow["TienSuBanThan"] is string) return (string)_DataRow["TienSuBanThan"];
                return "";
            }
            set
            {
                _DataRow["TienSuBanThan"] = value;
            }
        }
        #endregion

        #region string TienSuGiaDinh property
        public string TienSuGiaDinh
        {
            get
            {
                if (_DataRow["TienSuGiaDinh"] is string) return (string)_DataRow["TienSuGiaDinh"];
                return "";
            }
            set
            {
                _DataRow["TienSuGiaDinh"] = value;
            }
        }
        #endregion

        #region string DiUngThuoc property
        public string DiUngThuoc
        {
            get
            {
                if (_DataRow["DiUngThuoc"] is string) return (string)_DataRow["DiUngThuoc"];
                return "";
            }
            set
            {
                _DataRow["DiUngThuoc"] = value;
            }
        }
        #endregion

        #region string QuaTrinhBenhLy property
        public string QuaTrinhBenhLy
        {
            get
            {
                if (_DataRow["QuaTrinhBenhLy"] is string) return (string)_DataRow["QuaTrinhBenhLy"];
                return "";
            }
            set
            {
                _DataRow["QuaTrinhBenhLy"] = value;
            }
        }
        #endregion

        #region string KhamLamSang property
        public string KhamLamSang
        {
            get
            {
                if (_DataRow["KhamLamSang"] is string) return (string)_DataRow["KhamLamSang"];
                return "";
            }
            set
            {
                _DataRow["KhamLamSang"] = value;
            }
        }
        #endregion

        #region string NhipTim property
        public string NhipTim
        {
            get
            {
                if (_DataRow["NhipTim"] is string) return (string)_DataRow["NhipTim"];
                return "";
            }
            set
            {
                _DataRow["NhipTim"] = value;
            }
        }
        #endregion

        #region string NhipTho property
        public string NhipTho
        {
            get
            {
                if (_DataRow["NhipTho"] is string) return (string)_DataRow["NhipTho"];
                return "";
            }
            set
            {
                _DataRow["NhipTho"] = value;
            }
        }
        #endregion

        #region string NhietDo property
        public string NhietDo
        {
            get
            {
                if (_DataRow["NhietDo"] is string) return (string)_DataRow["NhietDo"];
                return "";
            }
            set
            {
                _DataRow["NhietDo"] = value;
            }
        }
        #endregion

        #region string CanNang property
        public string CanNang
        {
            get
            {
                if (_DataRow["CanNang"] is string) return (string)_DataRow["CanNang"];
                return "";
            }
            set
            {
                _DataRow["CanNang"] = value;
            }
        }
        #endregion

        #region string HuyetAp property
        public string HuyetAp
        {
            get
            {
                if (_DataRow["HuyetAp"] is string) return (string)_DataRow["HuyetAp"];
                return "";
            }
            set
            {
                _DataRow["HuyetAp"] = value;
            }
        }
        #endregion

        #region string ChieuCao property
        public string ChieuCao
        {
            get
            {
                if (_DataRow["ChieuCao"] is string) return (string)_DataRow["ChieuCao"];
                return "";
            }
            set
            {
                _DataRow["ChieuCao"] = value;
            }
        }
        #endregion

        #region int State property
        public int State
        {
            get
            {
                if (_DataRow["State"] is int) return (int)_DataRow["State"];
                return -1;
            }
            set
            {
                _DataRow["State"] = value;
            }
        }
        #endregion

        public HoSoKhamBenh(DataRow dataRow)
            : base(dataRow)
        {
        }

        public DanhMucKhoanMucHoSoKhamBenh _DanhSachKhoanMucHoSoKhamBenhClinic = new DanhMucKhoanMucHoSoKhamBenh();

        public void LoadKhoanMuc()
        {
            try
            {
                if (this == null) this.ID = Guid.Empty;
                _DanhSachKhoanMucHoSoKhamBenhClinic.LoadFromDB(this.ID);
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }
    }

    public class DanhMucHoSoKhamBenh : DanhMucDoiTuong, IEnumerable
    {
        #region Instance property: Singleton Design pattern
        private static DanhMucHoSoKhamBenh _Instance;
        public static DanhMucHoSoKhamBenh Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new DanhMucHoSoKhamBenh();
                    _Instance.DataView = _Instance.DataTable.DefaultView;
                    try
                    {
                        UniqueConstraint uniqueConstraint = new UniqueConstraint(_Instance.DataTable.Columns["Ma"]);
                        _Instance.DataTable.Constraints.Add(uniqueConstraint);
                        _Instance.DataView.Sort = "Ma DESC";
                    }
                    catch (Exception ex)
                    {
                    }
                }
                return _Instance;
            }
        }
        #endregion

        #region override TableName property
        public override string TableName
        {
            get
            {
                return "Clinic_HoSoKhamBenh";
            }
        }
        #endregion

        #region Methods required by IEnumerable (use for foreach)
        public IEnumerator GetEnumerator()
        {
            return new TokenEnumerator(this);
        }

        private class TokenEnumerator : IEnumerator
        {
            private int _Position = -1;
            DanhMucDoiTuong _Dstk;

            public TokenEnumerator(DanhMucDoiTuong dstk)
            {
                _Dstk = dstk;
            }

            // Declare the MoveNext method required by IEnumerator:
            public bool MoveNext()
            {
                if (_Position < _Dstk.DataView.Count - 1)
                {
                    _Position++;
                    return true;
                }
                else
                {
                    return false;
                }
            }

            // Declare the Reset method required by IEnumerator:
            public void Reset()
            {
                _Position = -1;
            }

            // Declare the Current property required by IEnumerator:
            public object Current
            {
                get
                {
                    return new HoSoKhamBenh((_Dstk.DataView[_Position].Row));
                }
            }
        }
        #endregion

        public HoSoKhamBenh this[int index]
        {
            get
            {
                if (0 <= index && index <= this.Count)
                {
                    return new HoSoKhamBenh(DataTable.Rows[index]);
                }
                else
                {
                    return null;
                }
            }
        }

        public HoSoKhamBenh NewDoiTuong()
        {
            try
            {
                return new HoSoKhamBenh(Instance.DataTable.NewRow());
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public HoSoKhamBenh GetByProperty(string property, object value)
        {
            try
            {
                DataRow[] rows = GetDataRows(property, value);
                if (rows.Length > 0) return new HoSoKhamBenh(rows[0]);
                else return null;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public HoSoKhamBenh GetByID(Guid objID)
        {
            try
            {
                return GetByProperty("ID", objID);
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public HoSoKhamBenh GetByMa(string ma)
        {
            try
            {
                return GetByProperty("Ma", ma);
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        //public DanhMucHoSoKhamBenh GetChuaThuTien(bool sinhTuDong)
        //{
        //    try
        //    {
        //        //Nếu sinh phiếu thu mà LoadFromDB --> Các phiếu trên RAM bị lỗi hết
        //        if (!sinhTuDong)
        //        {
        //            ChuanHoa_HoSoKhamBenhClinic_DaThuTienByChungTu();
        //            LoadFromDB();
        //        }

        //        string condition = "DaThuTien = 0 OR DaThuTien IS NULL";
        //        return Filter(condition);
        //    }
        //    catch (Exception ex)
        //    {
        //        if (ex is CustomException) throw ex;
        //        else
        //        {
        //            throw new Exception(ex.Message);
        //        }
        //    }
        //}

        public static DanhMucHoSoKhamBenh GetAll()
        {
            try
            {
                return Instance;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public DanhMucHoSoKhamBenh GetByLoaiHoSoKhamBenhID(Guid loaiHoSoKhamBenhID)
        {
            try
            {
                string filter = string.Format("LoaiHoSoKhamBenhID = '{0}'", loaiHoSoKhamBenhID);
                return Filter(filter);
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public DanhMucHoSoKhamBenh Filter(string filterExpression)
        {
            try
            {
                DanhMucHoSoKhamBenh obj = new DanhMucHoSoKhamBenh();
                obj.DataView = new DataView(DanhMucHoSoKhamBenh.Instance.DataTable, filterExpression, "Ma DESC", DataViewRowState.CurrentRows);
                return obj;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public void ChuanHoa_HoSoKhamBenh_DaXyLyByPhieuDangKi()
        {
            SqlConnection con = new SqlConnection(GeneralLibs.LocalSettings.ConnectString);
            try
            {
                con.Open();
                SqlCommand cmd = con.CreateCommand();
                cmd.CommandText = string.Format("UPDATE Clinic_HoSoKhamBenh SET DaThuTien = 1 WHERE ID IN ( \n" +
                                                "SELECT B.ID FROM ChungTu A INNER JOIN Clinic_HoSoKhamBenh B ON A.Clinic_HoSoKhamBenhID = B.ID ) \n" +
                                                "AND DaThuTien = 0 \n");
                cmd.ExecuteNonQuery();

                cmd.CommandText = string.Format("UPDATE Clinic_HoSoKhamBenh SET DaThuTien = 0 WHERE ID NOT IN ( \n" +
                                                "SELECT B.ID FROM ChungTu A INNER JOIN Clinic_HoSoKhamBenh B ON A.Clinic_HoSoKhamBenhID = B.ID ) \n" +
                                                "AND DaThuTien = 1 \n");
                cmd.ExecuteNonQuery();
            }
            catch (SqlException sqlEx)
            {
                throw new CustomException(CustomException.GetSqlExceptionMessage(sqlEx));
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
            finally
            {
                con.Close();
            }
        }
    }
}
